//
//  SLCollectionViewFlowLayout.m
//  Objective-C
//
//  Created by 陆承东 on 2019/2/20.
//  Copyright © 2019 S_Line. All rights reserved.
//

#import "SLCollectionViewFlowLayout.h"

@implementation SLCollectionViewFlowLayout

- (void)prepareLayout {
    [super prepareLayout];
}

//#pragma mark - cell的左右间距

- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect {
    
    NSMutableArray * answer = [[super layoutAttributesForElementsInRect:rect] mutableCopy];
    
    /* 处理左右间距 */
    for (int i = 1; i < [answer count]; ++i) {
        
        UICollectionViewLayoutAttributes *currentLayoutAttributes = answer[i];
        
        UICollectionViewLayoutAttributes *prevLayoutAttributes = answer[i - 1];
        
        //注意：currentLayoutAttributes.indexPath.section 这里是单独处理某个section的空隙，如果不需要可把if判断删除即可
        if (currentLayoutAttributes.indexPath.section == 0) {
            
            
            NSInteger origin = CGRectGetMaxX(prevLayoutAttributes.frame);
            
            if (origin + self.interitemSpacing + currentLayoutAttributes.frame.size.width < self.collectionViewContentSize.width) {
                
                CGRect frame = currentLayoutAttributes.frame;
                
                frame.origin.x = origin + self.interitemSpacing;
                
                currentLayoutAttributes.frame = frame;
                
            }
        }
    }
    
    return answer;
    
}

@end
